#include "Basic_Dynamic_lyvm.h"
#include <algorithm>
#include "xVMRelationStruct.h"

const int DDLG_X = 200;
const int DDLG_Y = 100;
const int DDLG_WIDTH = 300;
const int DDLG_WIDTH_BEGIN = 10;
const int DDLG_HEIGHT_BEGIN = 20;
const int DDLG_ENTRY_WIDTH = 120;
const int DDLG_ENTRY_HEIGHT = 20;
const int DDLG_WIDTH_GAP = DDLG_ENTRY_WIDTH + 20;
const int DDLG_HEIGHT_GAP = DDLG_ENTRY_HEIGHT + 10;
const int DDLG_DSCPT_WIDTH = DDLG_ENTRY_WIDTH + DDLG_WIDTH_GAP;
const int DDLG_DSCPT_HEIGHT = DDLG_ENTRY_HEIGHT * 2;

extern HINSTANCE _hInst ;  // this instance is defined in DIVA_MAIN
extern HWND _hWnd ;        // this instance is defined in DIVA_Application
extern string valuableNames;
extern bool bRefIn;
extern bool bRefOut;
extern int level;
extern bool bCancel_dyn;
extern bool bCancel_sta;

int ddlg_entry_count = 0;
int ddlg_id_begin = 100;
int ddlg_id_count = 1;

BOOL CALLBACK StaticLayoutDialog( HWND, UINT, WPARAM, LPARAM);

void Basic_Dynamic_lyvm::initial(DIVA_VM* startVM){
	/*TODO:change to Qt dialog*/
	//DialogBox( _hInst, "SLAYDIA", _hWnd, (DLGPROC)StaticLayoutDialog);
	if(bCancel_sta == true) return;
	_area = new Ly_influence_area(startVM, bRefIn, bRefOut, valuableNames, level);
	setupDialog();
	_ddlg.setWindowPos(
		DDLG_X,
		DDLG_Y,
		DDLG_WIDTH,
		DDLG_HEIGHT_BEGIN + DDLG_HEIGHT_GAP*ddlg_entry_count
		);
	showDialog();
	if(bCancel_dyn == true) return;
	setupParameters();
	clearDialog();
}

void Basic_Dynamic_lyvm::clearDialog(){
	_ddlg.clear();
}

void Basic_Dynamic_lyvm::addDescript(LPCSTR dscpt){
	_ddlg.addEntry("STATIC", dscpt, "string", ddlg_id_begin,
		WS_CHILD|WS_VISIBLE,
		DDLG_WIDTH_BEGIN, DDLG_HEIGHT_BEGIN + DDLG_HEIGHT_GAP*ddlg_entry_count,
		DDLG_DSCPT_WIDTH, DDLG_DSCPT_HEIGHT
		);
	ddlg_entry_count+=2;
}

void Basic_Dynamic_lyvm::addParameter(LPCSTR paraName, string paraType, LPCSTR default_value){
	_ddlg.addEntry("STATIC", paraName, "string", ddlg_id_begin,
		WS_CHILD|WS_VISIBLE,
		DDLG_WIDTH_BEGIN, DDLG_HEIGHT_BEGIN + DDLG_HEIGHT_GAP*ddlg_entry_count,
		DDLG_ENTRY_WIDTH, DDLG_ENTRY_HEIGHT
		);
	_ddlg.addEntry("EDIT", default_value, paraType, ddlg_id_begin + ddlg_id_count,
		WS_CHILD|WS_VISIBLE|WS_BORDER,
		DDLG_WIDTH_BEGIN+DDLG_WIDTH_GAP, DDLG_HEIGHT_BEGIN + DDLG_HEIGHT_GAP*ddlg_entry_count,
		DDLG_ENTRY_WIDTH, DDLG_ENTRY_HEIGHT
		);
	ddlg_id_count++;
	ddlg_entry_count++;

}

string Basic_Dynamic_lyvm::getParameter(string paraName){
	return _ddlg.getValue(paraName);
}

void Basic_Dynamic_lyvm::doLayout(DIVA_VM* startVM){
	initial(startVM);
	if(bCancel_sta || bCancel_dyn) return;

	layout(_area->getAllVMs(),startVM);
}

void Basic_Dynamic_lyvm::showDialog(){
	_ddlg.showDialog();
	ddlg_entry_count = 0;
	ddlg_id_count = 1;
}

void Basic_Dynamic_lyvm::setChanges(){
	positionmap::iterator it;
	bool invisible = false;
	vector<DIVA_VM*> theVisibleVM;
	for(it = _record.begin() ; it!= _record.end() ; ++it){
		DIVA_VM* target = (*it).first;
		if(!target->get_visible()){
			invisible = true;
		}
		else{
			theVisibleVM.push_back(target);
		}
	}

	if(!invisible){
		for(it = _record.begin() ; it!= _record.end() ; ++it){
			DIVA_VM* target = (*it).first;
			Ogre::Vector3  pos    = (*it).second;
			target->setPosition(pos);
		}
	}
	else{

	}
}

void Basic_Dynamic_lyvm::isAppliedToTargetVM(DIVA_VM* target){
	vector<DIVA_VM*> vms = _area->getAllVMs();
	vector<DIVA_VM*>::iterator it;
	it = find(vms.begin(),vms.end(),target);
	if(it != vms.end()){
		layout(_area->getAllVMs(),target);
		setChanges();
	}
	else{
		//do nothing , 目標VM不在layout範圍內
	}
}
